import os
from googletrans import Translator
import os
from googletrans import Translator

# 注意：http:// 而不是 https://
os.environ['HTTP_PROXY'] = 'http://127.0.0.1:7890'
os.environ['HTTPS_PROXY'] = 'http://127.0.0.1:7890'

class MultiLangTranslator:
    def __init__(self, proxy_host="127.0.0.1", proxy_port=7890, proxy_type="http"):
        # """
        # 初始化翻译器，并设置代理。
        # proxy_type: "http" 或 "socks5"
        # """
        self.translator = Translator()
        #
        # # 设置代理环境变量
        # proxy_url = f"{proxy_type}://{proxy_host}:{proxy_port}"
        # os.environ["HTTP_PROXY"] = proxy_url
        # os.environ["HTTPS_PROXY"] = proxy_url

    def translate(self, text, dest):
        """通用翻译方法"""
        result = self.translator.translate(text, dest=dest)
        return result.text

    # ====== 各语言翻译封装 ======
    def to_chinese(self, text):
        """翻译为中文"""
        return self.translate(text, dest="zh-CN")

    def to_english(self, text):
        """翻译为英文"""
        return self.translate(text, dest="en")

    def to_japanese(self, text):
        """翻译为日语"""
        return self.translate(text, dest="ja")

    def to_french(self, text):
        """翻译为法语"""
        return self.translate(text, dest="fr")

    def to_korean(self, text):
        """翻译为韩语"""
        return self.translate(text, dest="ko")
obj=MultiLangTranslator()
transcontent='''
山鉾行事は伝統と祭りの熱気を運ぶ行列だ
'''
re=obj.to_chinese(transcontent)
print(re)